<?php

if (!defined('TOBBIVM'))
	header('location:/template/notrepassing.php');
/**
 * This file is part of TobbiVMShop.
 *
 * TobbiVMShop is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TobbiVMShop is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TobbiVMShop.  If not, see <http://www.gnu.org/licenses/>.
 */
/**
 * Verify userdata from orderadress
 * if ok then save in database (=new Orderer) or display errors
 * used from cart.php
 *
 * @version    $Id$
 * @package    TobbiVM-Shop
 * @copyright  Copyright (C) 2012
 * @author     Norbert Gebert
 * @license    GPL3
 */
$mysqlBuffer = array();

if (COMPANY > 0)
{
	if ((COMPANY === 1 and !empty($_POST['companyx'])) or COMPANY === 2)
	{
		$_POST['companyx'] = $refValidate->testText($_POST['companyx'],
			_('The first name must consist only of letters'), 3, 35);
		$mysqlBuffer['orderer_company'] = $_POST['companyx'];
	}
}

if (FIRSTNAME > 0)
{
	if ((FIRSTNAME === 1 and !empty($_POST['firstnamex'])) or FIRSTNAME === 2)
	{
		$_POST['firstnamex'] = $refValidate->testText($_POST['firstnamex'],
			_('The first name must consist only of letters'), 3, 35);
		$mysqlBuffer['orderer_firstname'] = $_POST['firstnamex'];
	}
}
if (LASTNAME > 0)
{
	if ((LASTNAME === 1 and !empty($_POST['lastnamex'])) or LASTNAME === 2)
	{
		$_POST['lastnamex'] = $refValidate->testText($_POST['lastnamex'],
			_('The lastname must consist only of letters'), 3, 35);
		$mysqlBuffer['orderer_lastname'] = $_POST['lastnamex'];
	}
}

if (STREET > 0)
{
	if ((STREET === 1 and !empty($_POST['streetx'])) or STREET === 2)
	{
		$_POST['streetx'] = $refValidate->testText($_POST['streetx'],
			_('The street name must consist only of letters'), 3, 35);
		$mysqlBuffer['orderer_street'] = $_POST['streetx'];
	}
}

if (ZIP_CODE > 0)
{
	if ((ZIP_CODE === 1 and !empty($_POST['zipx'])) or ZIP_CODE === 2)
	{
		$_POST['zipx'] = $refValidate->testNumber($_POST['zipx'],
			_('The zip-code may consist of only digits'), ZIP_CODE_LENGH, ZIP_CODE_LENGH);
		$mysqlBuffer['orderer_zip'] = $_POST['zipx'];
	}
}

if (CITY > 0)
{
	if ((CITY === 1 and !empty($_POST['cityx'])) or CITY === 2)
	{
		$_POST['cityx'] = $refValidate->testText($_POST['cityx'],
			_('The city name must consist only of letters'), 2, 35);
		$mysqlBuffer['orderer_city'] = $_POST['cityx'];
	}
}

if (COUNTRY > 0)
{
	if ((COUNTRY === 1 and !empty($_POST['countryx'])) or COUNTRY === 2)
	{
		$_POST['countryx'] = $refValidate->testText($_POST['countryx'],
			_('The country name must consist only of letters'), 3, 50);
		$mysqlBuffer['orderer_country'] = $_POST['countryx'];
	}
}

$mysqlBuffer['orderer_state'] = DEFAULT_STATE;

if (PHONE > 0)
{
	if ((PHONE === 1 and !empty($_POST['phonex'])) or PHONE === 2)
	{
		$_POST['phonex'] = $refValidate->testPhonenumber($_POST['phonex'],
			_('The phone number format is incorrect'));
		$mysqlBuffer['orderer_phone'] = $_POST['phonex'];
	}
}

if (FAX > 0)
{
	if ((FAX === 1 and !empty($_POST['faxx'])) or FAX === 2)
	{
		$_POST['faxx'] = $refValidate->testPhonenumber($_POST['faxx'],
			_('The fax number format is incorrect'));
		$mysqlBuffer['orderer_fax'] = $_POST['faxx'];
	}
}

if (MOBILE > 0)
{
	if ((MOBILE === 1 and !empty($_POST['handyx'])) or MOBILE === 2)
	{
		$_POST['handyx'] = $refValidate->testNumber($_POST['handyx'],
			_('The mobile number consist of only digits'));
		$mysqlBuffer['orderer_handy'] = $_POST['handyx'];
	}
}

if (EMAIL > 0)
{
	if ((EMAIL === 1 and !empty($_POST['emailx'])) or EMAIL === 2)
	{
		$_POST['emailx'] = $refValidate->testEmail($_POST['emailx'],
			_('The email format is incorrect'));
		$mysqlBuffer['orderer_email'] = $_POST['emailx'];
	}
}

if (message::ifErrorMessage())
{ // Error in Userdata
	message::setErrorRow(_('Please correct the problems listed:'), TRUE);
	$mysqlBuffer = NULL;
}
else
{
	//$mysqlBuffer .= "customer_name ='" . $_POST['namex'] . "', ";
	$mysqlBuffer['orderer_order_date'] = date("Y-m-d H:i:s");
	//$mysqlBuffer .= "orderer_ip ='" . get_client_ip() . "' ";


	db::getInstance()->rawQuery("INSERT INTO orderer (orderer_comment) VALUES ('New Guest')");
	// Update the database
	db::getInstance()->where('orderer_comment', 'New Guest');
	
	if (db::getInstance()->update('orderer', $mysqlBuffer))
	{
		db::getInstance()->where('orderer_comment', 'New Guest');
		$result = db::getInstance()->getOne("orderer");
		$_SESSION['ordererIdx'] = $result['orderer_id'];

		$data = Array('orderer_comment' => ' ');
		db::getInstance()->where('orderer_comment', 'New Guest');
		db::getInstance()->update('orderer', $data);
	}
	else
	{
		// Error in Userdata
		message::setErrorRow(_('Error occured by saving adress!'), TRUE);
	}
	$mysqlBuffer = NULL;
	//$pagefile = 'module/cart/cart.ordereradress.php';
	//if (is_file($pagefile))
	//	include_once($pagefile);
}

/*
 * Returns the client-ip-adress
 */

function get_client_ip()
{
	$ipaddress = '';
	if (getenv('HTTP_CLIENT_IP'))
	{
		$ipaddress = getenv('HTTP_CLIENT_IP');
	}
	else if (getenv('HTTP_X_FORWARDED_FOR'))
	{
		$ipaddress = getenv('HTTP_X_FORWARDED_FOR');
	}
	else if (getenv('HTTP_X_FORWARDED'))
	{
		$ipaddress = getenv('HTTP_X_FORWARDED');
	}
	else if (getenv('HTTP_FORWARDED_FOR'))
	{
		$ipaddress = getenv('HTTP_FORWARDED_FOR');
	}
	else if (getenv('HTTP_FORWARDED'))
	{
		$ipaddress = getenv('HTTP_FORWARDED');
	}
	else if (getenv('REMOTE_ADDR'))
	{
		$ipaddress = getenv('REMOTE_ADDR');
	}
	else
	{
		$ipaddress = 'UNKNOWN';
	}

	return $ipaddress;
}

?>